<?php
	require('config.php');
	$email = $_POST["email"];
	$passwd = $_POST["password"];
	$stmt = $connection->prepare("select * from user_id_passwd_tobeconfirm where id=?");
	$stmt->bind_param("s", $email);
	$stmt->execute();
	$stmt->store_result();
	$row_cnt = $stmt->num_rows;
	$uuid = "";
	if ($row_cnt == 0)
	{
		$uuid = substr(guid(), 1, 36);
		$stmt = $connection->prepare("insert into user_id_passwd_tobeconfirm (id, passwd, uuid) values (?, ?, ?)");
		$stmt->bind_param("sss", $email, $passwd, $uuid);
		$stmt->execute();
	}
	else
	{
		$stmt->bind_result($e, $p, $uuid);
		$stmt->fetch();
		$stmt = $connection->prepare("update user_id_passwd_tobeconfirm set passwd=?, uuid=? where id=?");
		$stmt->bind_param("sss", $passwd, $uuid, $email);
		$stmt->execute();
	}
	sendEmail($email, $uuid);
	
	function guid()
	{
		if (function_exists('com_create_guid')){
			return com_create_guid();//window下
		}else{
			mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
			$charid = strtoupper(md5(uniqid(rand(), true)));
			$hyphen = chr(45);// "-"
			$uuid = chr(125)// "{"
					.substr($charid, 0, 8).$hyphen
					.substr($charid, 8, 4).$hyphen
					.substr($charid,12, 4).$hyphen
					.substr($charid,16, 4).$hyphen
					.substr($charid,20,12)
					.chr(123);// "}"
			return $uuid;
		}
	}
	
	function sendEmail($e, $u)
	{
		require("class.phpmailer.php");
		
		$mail = new PHPMailer();
		
		$mail->IsSMTP(); // set mailer to use SMTP
		$mail->Mailer = "smtp";
		$mail->CharSet = "UTF-8";
		$mail->Host = "smtp.163.com";
		$mail->Port = 25;
		$mail->SMTPAuth = true; // turn on SMTP authentication
		$mail->Username = "videobook@163.com"; // SMTP username
		$mail->Password = "10126301"; // SMTP password 
		 
		$mail->From = "videobook@163.com";
		$mail->AddAddress($e); 
		 
		$mail->Subject  = "激活";
		$mail->IsHTML(true);
		$mail->Body = " <div style='position:relative;width:500px; height:400px; background-color:#DDD;padding-left:20px;padding-right:10px;'> <div style='position:relative;margin-left:auto;margin-right:auto;width:200px;height:100px;'><img src='http://shiliu.tv/ico/logo4.png'/></div> <div> <p style='font: normal 16px Helvetica,Arial,sans-serif;font-weight:bold;'>欢迎来到石榴tv！<br/><br/> 您的登录名为: <a href='mailto:" . $e . "'>" . $e . "</a><br/><br/> 点击以下链接，激活您的石榴tv账号！<br/><br/><a href='http://shiliu.tv/regist_activation.php?uuid=" . $u . "'>http://shiliu.tv/regist_activation.php?uuid=" . $u ."</a><br/><br/></p> <p style='font: normal 12px Helvetica,Arial,sans-serif;font-weight:bold;color:#444;'>如果以上链接无法访问，请将该网址复制并粘贴至新的浏览器窗口中。<br/><br/> 这只是一封系统自动发出的邮件，请不要直接回复。<br/><br/>如果你错误的收到了本电子邮件，请您忽略上述内容</p> </div> </div>";
        $mail->FromName = "石榴网";
		$mail->WordWrap = 50;  
		 
		if(!$mail->Send()) {
			echo 'Message was not sent.';
			echo 'Mailer error: ' . $mail->ErrorInfo;
		} else {
			echo 'Message has been sent.';
        }
	}
?>
